package o;

import androidx.annotation.NonNull;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public final class aak extends FilterInputStream {
    private int ˊ;
    private final long ˏ;

    @NonNull
    public static InputStream ˊ(@NonNull InputStream inputStream, long j) {
        return new aak(inputStream, j);
    }

    private aak(@NonNull InputStream inputStream, long j) {
        super(inputStream);
        this.ˏ = j;
    }

    public synchronized int available() throws IOException {
        return (int) Math.max(this.ˏ - ((long) this.ˊ), (long) this.in.available());
    }

    public synchronized int read() throws IOException {
        int read;
        read = super.read();
        ˏ(read >= 0 ? 1 : -1);
        return read;
    }

    public int read(byte[] bArr) throws IOException {
        return read(bArr, 0, bArr.length);
    }

    public synchronized int read(byte[] bArr, int i, int i2) throws IOException {
        return ˏ(super.read(bArr, i, i2));
    }

    private int ˏ(int i) throws IOException {
        if (i >= 0) {
            this.ˊ += i;
        } else if (this.ˏ - ((long) this.ˊ) > 0) {
            throw new IOException("Failed to read all expected data, expected: " + this.ˏ + ", but read: " + this.ˊ);
        }
        return i;
    }
}
